﻿using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
using log4net;
using System.Reflection;
using Microsoft.ApplicationBlocks.Data;
using System.Text.RegularExpressions;

namespace AnyOffice.general.mobile
{
    public partial class _new : ModulePage
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                try
                {
                    UserCache cache = new UserCache();
                    this.ViewState["UserName"] = cache.UserName;
                    ConfigManager manager = new ConfigManager();
                    string mobileOutPriv = manager.MobileOutPriv;
                    mobileOutPriv = "," + mobileOutPriv + ",";
                    bool flag = true;
                    if (mobileOutPriv.IndexOf("," + base.User.Identity.Name + ",") == -1)
                    {
                        flag = false;
                        this.pnlMobile.Visible = false;
                        this.litNavTitel.Text = "<span title=\"不允许向OA系统外人员发手机短信的\">（无外发权限）</span>";
                    }
                    else if (Request.QueryString["mobile"] != null)
                    {
                        this.txtMobile.Value = Request.QueryString["mobile"].ToString() + ",";
                    }
                    this.ViewState["IsMobile"] = flag;
                    if ((Request.QueryString["to_id"] != null) && (Request.QueryString["to_name"] != null))
                    {
                        this.ToId.Value = Request.QueryString["to_id"].ToString() + ",";
                        this.ToName.Value = Request.QueryString["to_name"].ToString() + ",";
                    }
                    object obj2 = SqlHelper.ExecuteScalar(base.Conn, CommandType.Text, "SELECT GETDATE()");
                    if (obj2 != null)
                    {
                        this.txtSendTime.Value = Convert.ToDateTime(obj2).ToString();
                    }
                }
                catch (Exception exception)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("", exception);
                    }
                }
            }
            if (this.Page.IsPostBack)
            {
                this.pnlMain.Visible = false;
                this.pnlLater.Visible = true;
                MobileDao dao = new MobileDao();
                MobileData data = new MobileData();
                data.FromId = base.User.Identity.Name;
                data.Content = this.txtContent.Value;
                data.SendTime = Convert.ToDateTime(this.txtSendTime.Value);
                data.SendFlag = 0;
                if (this.ToId.Value != string.Empty)
                {
                    try
                    {
                        DataTable table = dao.SendToIds(base.Conn, this.ToId.Value, data);
                        if (table.Rows.Count > 0)
                        {
                            string str2 = string.Empty;
                            for (int i = 0; i < table.Rows.Count; i++)
                            {
                                str2 = str2 + table.Rows[i]["UserName"].ToString() + ",";
                            }
                            if (str2.EndsWith(","))
                            {
                                str2 = str2.Substring(0, str2.Length - 1);
                            }
                            this.litNoMobile.Text = base.ShowMessage("以下用户没有设置手机号码：" + str2);
                        }
                    }
                    catch (Exception exception2)
                    {
                        if (log.IsErrorEnabled)
                        {
                            log.Error("", exception2);
                        }
                    }
                }
                if (Convert.ToBoolean(this.ViewState["IsMobile"].ToString()) && (this.txtMobile.Value != string.Empty))
                {
                    try
                    {
                        string[] strArray = this.txtMobile.Value.Split(new char[] { ',' });
                        string str3 = string.Empty;
                        string pattern = @"1\d{10}|106\d{10,12}|\d{7,}";
                        Regex regex = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                        for (int j = 0; j < strArray.Length; j++)
                        {
                            if (regex.Match(strArray[j]).Success)
                            {
                                data.Phone = strArray[j];
                                dao.Insert(base.Conn, data);
                            }
                            else
                            {
                                str3 = str3 + strArray[j] + ",";
                            }
                        }
                        if (str3.EndsWith(","))
                        {
                            str3 = str3.Substring(0, str3.Length - 1);
                        }
                        if (str3 != string.Empty)
                        {
                            this.litMobileError.Text = base.ShowMessage("以下手机号码错误：" + str3);
                        }
                    }
                    catch (Exception exception3)
                    {
                        if (log.IsErrorEnabled)
                        {
                            log.Error("", exception3);
                        }
                    }
                }
                this.litMessage.Text = base.ShowMessage("短信已提交至短信服务器，正在后台进行发送，您可以继续进行其它工作");
            }
        } 

    }
}
